﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Bling.DSL;
using Bling.WPF;
using Bling.Util;

namespace Bling.Tutorial {
  public class MetaBall : Canvas {
    public MetaBall() {
      CanvasBl canvas = this;
      new CanvasBl(canvas) {
        Size = canvas.Size, LeftTop = new Point(0, 0),
        Background = Brushes.Black,
        Effect = {
          Custom = (input, uv) => {
            Random r = new Random();
            ColorBl[] colors = new ColorBl[] {
            Colors.Red, Colors.Blue,
            Colors.Green, Colors.Cyan,
            Colors.Orange, Colors.Yellow,
          };
            ColorBl color = Colors.Black;
            DoubleBl metaball = 0;
            var sz = canvas.Size;
            foreach (ColorBl primary in colors) {
              PointBl point = new ThumbBl(canvas) {
                Background = primary,
                ZIndex = 1,
                CenterPosition = canvas.CenterSize - new PointBl(150, 150) +
                                 r.NextPoint() * new PointBl(300, 300),
                CanDrag = true,
              };
              var v = (uv * canvas.Size - point);
              v = v * v;
              var at = 3d / (v.X + v.Y);
              color += primary * at;
              metaball += at;
            }
            var useColor = color / metaball;
            return (metaball > .0016).Condition(useColor, Colors.White);
          }
        },
      };
    }

  }

}
